//引入第三方express模块
const express=require("express");
//引入第三方路由器模块
const router=express.Router();
//引入连接池
const pool=require("../pool")

//创建路由，端口
router.get("/",(req,res)=>{
  var pno=req.query.pno
  var sql1="select pid from product_list";
  var productAll=0
  pool.query(sql1,(err,result)=>{
    if(err) throw err;
    productAll=result.length;
  })
  console.log(pno)
  var sql="select * from product_list limit ?,10"
  if(!pno){pno=1}
  var offset=(pno-1)*10;
  pool.query(sql,[offset],(err,result)=>{
    if(err) console.log(err);
    res.send({count:productAll,data:result})
  })
})
router.post("/add",(req,res)=>{
  console.log(req.body)
  var $pname=req.body.pname;
  var $blending_rate=req.body.blending_rate;
  var $elending_rate=req.body.elending_rate;
  var $debt_maturity=req.body.debt_maturity;
  var $minlend_money=req.body.minlend_money;
  var $family_name=req.body.family_name;

  var sql="INSERT INTO product_list VALUES(null,?,?,?,?,?,?)";
  pool.query(sql,[$pname,$blending_rate,$elending_rate,$debt_maturity,$minlend_money,$family_name],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows>0){
      res.send('200');
    }
  })
})

router.get("/del",(req,res)=>{
  console.log(req.query)
  var $pid=req.query.pid
  var sql="delete from product_list where pid=?";
  pool.query(sql,[$pid],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows>0){
      res.send('200');
    }
  })
});




router.post("/update",(req,res)=>{
  console.log(req.body)
  var $pid=req.body.pid
  var $pname=req.body.pname;
  var $blending_rate=req.body.blending_rate;
  var $elending_rate=req.body.elending_rate;
  var $debt_maturity=req.body.debt_maturity;
  var $minlend_money=req.body.minlend_money;
  var $family_name=req.body.family_name;
  //console.log($pid)
  var sql="update product_list set pname=?,blending_rate=?,elending_rate=?,debt_maturity=?,minlend_money=?,family_name=? where pid=?"
  pool.query(sql,[$pname,$blending_rate,$elending_rate,$debt_maturity,$minlend_money,$family_name,$pid],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows>0){
      res.send('200');
    }
  })
})

// router.post('/update',(req,res)=>{
//   var result=req.body;
//   var uid=result.uid;
//   var uname=result.uname;
//   var upwd=result.upwd;
//   var uphone=result.uphone;
//   var uemail=result.uemail;
//   console.log(uid,uname,upwd,uphone,uemail);
// })
//公开模块
module.exports=router;
